﻿package com.kaka.web_maven.dao;

import com.kaka.web_maven.entity.Adimgs;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AdimgsDao {
    /**
     * 返回所有的用户
     */
    public List<Adimgs> getList(){
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 获取数据库的链接
            Connection conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/test_maven?useUnicode=true&characterEncoding=utf8",
                    "root", "");
            // 准备pstmt对象

            PreparedStatement pstmt =  conn.prepareStatement("SELECT * from adimgs  ORDER BY img_order ASC LIMIT 0,4");
            // 执行sql语句
            ResultSet rs =  pstmt.executeQuery();


            // 循环获取数据
            List<Adimgs> list = new ArrayList<>();
            while (rs.next()){
                Adimgs img = new Adimgs();
                img.setId(rs.getInt("id"));
                img.setImg_desc(rs.getString("img_desc"));
                img.setImg_url(rs.getString("img_url"));
                img.setImg_page(rs.getString("img_page"));
                img.setImg_order(rs.getInt("img_order"));
                list.add(img);
            }


            rs.close();
            conn.close();
            return list;
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }
}
